package com.mbrs.repo;

import com.mbrs.aggr.MyUser;
import dml.users.repo.UserRepository;
import erp.mongodb.MongodbRepository;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

/**
 * @author zheng chengdong
 */
public class MyUserRepository extends MongodbRepository<MyUser, Long> implements UserRepository<MyUser, Long> {
    public MyUserRepository(MongoTemplate mongoTemplate) {
        super(mongoTemplate);
    }

    public Long findMaxUserId() {
        if (mongoTemplate == null) {
            return null;
        }
        Query query = new Query();
        query.with(Sort.by(Sort.Direction.DESC, "_id"));
        query.limit(1);
        MyUser maxEtt = mongoTemplate.findOne(query, MyUser.class);
        if (maxEtt == null) {
            return null;
        }
        return (Long) maxEtt.getId();
    }
}
